GBML manual / Arrays

array_shuffle

Mixes up the order of the places in target, in place: afterwards it holds the same values in a random order. It makes no copy. Any kind of value can be shuffled, text included.

Syntax

array_shuffle(target)

Arguments

NameType
targetarray

Returns

Nothing.

Description

Mixes up the order of the places in target, an array. Afterwards the array holds exactly the same values as before, in a random order.

array_shuffle(order);

It changes the array itself

array_shuffle moves the values that are already in target. It does not make a new array, and it needs no extra memory. The old order is gone afterwards. If you want to keep it, make a copy with array_copy first and shuffle the one you want mixed up.

It mixes the places the array really has, the number array_length gives you. An array never gains or loses a place, so it has the same places afterwards.

Any kind of value can be shuffled: numbers, text, structs, references. Nothing is compared, so there is no rule about what the array holds. (A sort is different: it only works on numbers and text.)

Where the random numbers come from

A shuffle picks its random numbers from the same place roll_int does: one stream of random numbers for the whole game. There is no seed to give the shuffle itself. What follows from that:

Every possible order can come up. It is very slightly more likely to land on some orders than others, because a random number is cut down to fit a small range, but the difference is far too small for a game to show.

What happens in the awkward cases

You do thisWhat happens
shuffle an array with one placenothing changes, and no random numbers are used
shuffle an array that has the same value more than onceworks as normal. Equal values are interchangeable, so you cannot tell which is which
shuffle and hope a value movesit may stay where it was. That can happen with random order, and it gets less likely the more places there are
give it a list or a gridrefused when checked, and the message names the function that shuffles that kind, like list_shuffle
use it where a value is neededrefused when checked. It gives nothing back

In a script

A script that takes an array as a parameter works on the caller's own array (see Arrays). So array_shuffle inside a script shuffles the caller's array, whatever its size.

What it costs

Nothing is set aside: the values are swapped where they stand, one swap for each place after the first. No timing was measured.

Example

var order = array_create(4, 0);
order[0] = 1;
order[1] = 2;
order[2] = 3;
order[3] = 4;
array_shuffle(order);    // the same four numbers, in a new order
var first = order[0];    // whichever one landed first

On the GBA

Shuffles the array_length(target) places the array has. A Fisher-Yates shuffle done in place: it makes no copy and needs no extra memory. For each place from the last down to the second, it swaps that place with one picked at random from the places up to and including it, so every order can come up. The pick comes from gbml::roll_int, the one stream the roll_ functions use, so a shuffle follows the same rules: the same ROM shuffles the same way every time it is switched on, roll_reseed changes that, and roll_reseed with the same number just before a shuffle gives the same shuffle again. A shuffle uses up rolls from that stream (one for each place after the first), so it moves every later roll along. Nothing is compared, so any kind of item can be shuffled.

See also: list_shufflegrid_shufflearray_sortarray_copyarray_lengthroll_introll_reseedArraysData containers